<?php

namespace App\Jobs\Train\TrainOrder;

use App\Models\Train\TrainOrders\Repositories\Interfaces\TrainOrdersRepositoryInterface;
use App\Models\Train\TrainOrders\TrainOrder;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ProcessTrainOrderAutoCancel implements ShouldQueue, ShouldBeUnique
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private TrainOrder $train_order;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(TrainOrder $order)
    {
        $this->train_order = $order;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $trainOrderRepo = app(TrainOrdersRepositoryInterface::class);
        if ($this->train_order->isCancel()) {
            return;
        }
        $trainOrderRepo->cancelOrder($this->train_order, false);
    }

    /**
     * 任务的唯一 ID。
     *
     * @return string
     */
    public function uniqueId()
    {
        return $this->train_order->getOrderSn();
    }


    /**
     * 计算重试任务之前要等待的秒数。
     *
     * @return array
     */
    public function backoff()
    {
        return [60, 120];
    }
}
